<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="专业名称" prop="specialtyName">
        <el-input
          v-model="queryParams.specialtyName"
          placeholder="请输入专业名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="专业代码" prop="specialtyCode">
        <el-input
          v-model="queryParams.specialtyCode"
          placeholder="请输入专业代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="院校名称" prop="schoolName">
        <el-input
          v-model="queryParams.schoolName"
          placeholder="请输入院校名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="院校代码" prop="schoolCode">
        <el-input
          v-model="queryParams.schoolCode"
          placeholder="请输入院校代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="年份" prop="year">
         <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.year"
                        type="year"
                        value-format="yyyy"
                        placeholder="选择年份">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="专科或本科" prop="uOrS">
        <el-select v-model="queryParams.uOrS" clearable size="small" placeholder="请选择本科专科" @change="handleQuery">
          <el-option
            v-for="item in education"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="招生地区" prop="admissionsAddress">
        <el-select v-model="queryParams.admissionsAddress" filterable clearable size="small" placeholder="请选择招生地区" @change="handleQuery">
          <el-option
            v-for="item in provinceName"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:CompassChangeSubject:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <importData
          v-hasPermi="['system:CompassChangeSubject:edit']"
          title="选科要求"
          action="CompassChangeSubject"
          @success="getList"
        >
        </importData>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:CompassChangeSubject:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:CompassChangeSubject:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="CompassChangeSubjectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="专业名称" align="center" prop="specialtyName">
        <template #default="{row}">
          <div>{{row.specialtyName}}</div>
          <div>{{row.specialtyCode}}</div>
        </template>
      </el-table-column>
      <!--<el-table-column label="专业代码" align="center" prop="specialtyCode" />-->
      <el-table-column label="院校名称" align="center" prop="schoolName">
        <template #default="{row}">
          <div>{{row.schoolName}}</div>
          <div>{{row.schoolCode}}</div>
        </template>
      </el-table-column>
      <!--<el-table-column label="院校代码" align="center" prop="schoolCode" />-->
      <el-table-column label="专业排名" align="center" prop="specialtySort" />
      <el-table-column label="年份" align="center" prop="year" />
      <el-table-column label="招生地区" align="center" prop="admissionsAddress" />
      <el-table-column label="专科或本科" align="center" prop="uorS" />
      <!--<el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="首选科目要求" align="center" prop="firstCondition" />
      <el-table-column label="再选科目要求" align="center" prop="secCondition" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:CompassChangeSubject:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:CompassChangeSubject:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改科目要求信息表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="专业名称" prop="specialtyName">
          <el-input v-model="form.specialtyName" placeholder="请输入专业名称" />
        </el-form-item>
        <el-form-item label="专业代码" prop="specialtyCode">
          <el-input v-model="form.specialtyCode" placeholder="请输入专业代码" />
        </el-form-item>
        <el-form-item label="院校名称" prop="schoolName">
          <!--<el-input v-model="form.schoolName" placeholder="请输入院校名称" />-->
          <el-autocomplete
            popper-class="my-autocomplete"
            v-model="form.schoolName"
            :fetch-suggestions="queryAutoSearchAsync"
            placeholder="请输入内容"
            @select="handleAutoSelect"
          >
            <template slot-scope="{ item }">
              <div v-if="item.schoolCode != -1">
                <div class="name">{{ item.schoolName }}</div>
                <span class="addr">{{ item.schoolCode }}</span>
              </div>
              <div v-else>
                <el-button type="text">暂无匹配项，去添加</el-button>
              </div>
            </template>
          </el-autocomplete>
        </el-form-item>

        <!--<el-form-item label="院校代码" prop="schoolCode">
          <el-input v-model="form.schoolCode" placeholder="请输入院校代码" />
        </el-form-item>-->
        <el-form-item label="招生地区" prop="admissionsAddress">
          <el-select v-model="form.admissionsAddress" filterable clearable size="small" placeholder="请选择招生地区">
            <el-option
              v-for="item in provinceName"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年份" prop="year">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.year"
                          type="year"
                          value-format="yyyy"
                          placeholder="选择年份">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="专科或本科" prop="uOrS">
          <el-select v-model="form.uorS" clearable size="small" placeholder="请选择本科专科">
            <el-option
              v-for="item in education"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专业排名" prop="specialtySort">
          <el-input v-model="form.specialtySort" type="number" placeholder="请输入专业排名" />
        </el-form-item>
        <el-form-item label="首选科目要求" prop="firstCondition">
          <el-input v-model="form.firstCondition" :autosize="{minRows: 4}" type="textarea" placeholder="请输入首选科目要求" />
        </el-form-item>
        <el-form-item label="再选科目要求" prop="secCondition">
          <el-input v-model="form.secCondition" :autosize="{minRows: 4}" type="textarea" placeholder="请输入再选科目要求" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listCompassChangeSubject, getCompassChangeSubject, delCompassChangeSubject, addCompassChangeSubject, updateCompassChangeSubject, exportCompassChangeSubject } from "@/api/system/CompassChangeSubject";

  import dictMixins from "@/views/mixins/dictMixins";
  export default {
    mixins: [dictMixins],

    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 科目要求信息表表格数据
        CompassChangeSubjectList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          specialtyName: null,
          schoolCode: null,
          year: null,
          specialtyCode: null,
          uOrS: null,
          schoolName: null,
          admissionsAddress: null,
        },
        // 表单参数
        form: {
          schoolName: '',
          schoolCode: ''
        },
        // 表单校验
        rules: {},
        provinceName: [],
        education: []
      };
    },
    async created() {
      this.getList();
      this.provinceName = await this.getDictByTypeName('provinceName')
      this.education = await this.getDictByTypeName('education')
    },
    methods: {
      /** 查询科目要求信息表列表 */
      getList() {
        this.loading = true;
        listCompassChangeSubject(this.queryParams).then(response => {
          this.CompassChangeSubjectList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          specialtyName: null,
          schoolCode: null,
          year: null,
          createTime: null,
          specialtyCode: null,
          uOrS: null,
          id: null,
          specialtySort: null,
          firstCondition: null,
          schoolName: null,
          admissionsAddress: null,
          updateTime: null,
          secCondition: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加科目要求信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getCompassChangeSubject(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改科目要求信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateCompassChangeSubject(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addCompassChangeSubject(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除科目要求信息表编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCompassChangeSubject(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有科目要求信息表数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCompassChangeSubject(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      },

      // 导入
      handleUpload() {

      }
    }
  };
</script>

<style lang="scss">
  .my-autocomplete {
    li {
      line-height: normal;
      padding: 7px;

      .name {
        text-overflow: ellipsis;
        overflow: hidden;
      }

      .addr {
        font-size: 12px;
        color: #b4b4b4;
      }

      .highlighted .addr {
        color: #ddd;
      }
    }
  }
</style>
